from http.server import BaseHTTPRequestHandler, HTTPServer
import time

class TimeServerHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            self.send_response(200)
            self.send_header('Content-type', 'text/plain')
            self.end_headers()

            current_time = str(time.time()).encode('utf-8')
            self.wfile.write(current_time)

        else:
            self.send_response(404)

if __name__ == '__main__':
    host = 'localhost'
    port = 8000

    try:
        server = HTTPServer((host, port), TimeServerHandler)
        print('HTTP服务器正在运行，监听地址：%s，端口号：%s' % (host, port))
        server.serve_forever()

    except KeyboardInterrupt:
        print('^C 接收到键盘中断，停止HTTP服务器')
        server.server_close()